package durst.action.account;

import java.sql.SQLException;
import java.util.List;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import durst.action.CalendarBasicAction;
import durst.data.Invite;
import durst.data.dao.DaoFactory;
import durst.data.dao.impl.InviteDao;

@UrlBinding("/action/account/{$event}/{invites[0].event.id}/{invites[0].invitee.id}")
public class DeclineInvitationAction extends CalendarBasicAction {
	@ValidateNestedProperties({ @Validate(field = "event.id", required = true),
			@Validate(field = "invitee.id", required = true, on = "remove") })
	private List<Invite> invites;

	@DefaultHandler
	@SuppressWarnings("unchecked")
	public Resolution decline() throws SQLException {
		InviteDao<Invite, Integer> dao = (InviteDao<Invite, Integer>) DaoFactory.getInstance()
				.createDao(Invite.class);
		dao.declineInvites(context.getAccount(), invites);
		return new RedirectResolution(ManageAccountActionBean.class);
	}

	@SuppressWarnings("unchecked")
	public Resolution revoke() throws SQLException {
		InviteDao<Invite, Integer> dao = (InviteDao<Invite, Integer>) DaoFactory.getInstance()
				.createDao(Invite.class);
		dao.revokeInvites(context.getAccount(), invites);
		return new ForwardResolution("/blank.jsp");
	}

	public void setInvites(List<Invite> invites) {
		this.invites = invites;
	}

	public List<Invite> getInvites() {
		return invites;
	}
}
